/*==============================================================================
            Copyright (c) 2010-2011 QUALCOMM Incorporated.
            All Rights Reserved.
            Qualcomm Confidential and Proprietary
==============================================================================*/

package com.EdificacioAR;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

/** Texture is a support class for the QCAR samples applications.
 * 
 *  Exposes functionality for loading a texture from the APK.
 *  
 * */

public class Texture
{
    public int mWidth;      /// The width of the texture.
    public int mHeight;     /// The height of the texture.
    public int mChannels;   /// The number of channels.
    public byte[] mData;    /// The pixel data.
    
    /** Returns the raw data */
    public byte[] getData()
    {
        return mData;
    }    
    
    /** Factory function to load a texture from the SD Card or similar. */
    public static Texture loadTextureFromExternalStorage(String filename) {
    	
    	try {
			InputStream is = new FileInputStream(filename);
			return loadTexture(is);
		} catch (FileNotFoundException e) {
			DebugLog.LOGE("Texture - loadTextureFromExternalStorage Error: File '" + filename + "' not found");
			e.printStackTrace();
			return null;
		}
    }
    
    /** Factory function to load a texture from the APK. */
    public static Texture loadTextureFromApk(String filename, AssetManager assets)
    {
    	try {
			InputStream is = assets.open(filename, AssetManager.ACCESS_BUFFER);
			return loadTexture(is);
		} catch (IOException e) {
			DebugLog.LOGE("Texture - loadTextureFromAPK IO Error with File '" + filename + "'");
			e.printStackTrace();
			return null;
		}
    }
    
    /** Factory function to load a texture from an input stream */
    private static Texture loadTexture(InputStream inputStream)
    {
        DebugLog.LOGD("Texture::loadTextureFromApk - start loading Texture");
		
		//inputStream = assets.open(fileName, AssetManager.ACCESS_BUFFER);
		 
		BufferedInputStream bufferedStream = new BufferedInputStream(inputStream);
		Bitmap bitMap = BitmapFactory.decodeStream(bufferedStream);
		DebugLog.LOGD("Texture::loadTextureFromApk - w: " + bitMap.getWidth() + " , h: " + bitMap.getHeight());
		
		int[] data = new int[bitMap.getWidth() * bitMap.getHeight()];
		bitMap.getPixels(data, 0, bitMap.getWidth(), 0, 0,
		                    bitMap.getWidth(), bitMap.getHeight());
		
		// Convert:
		byte[] dataBytes = new byte[bitMap.getWidth() *
		                           bitMap.getHeight() * 4];
		for (int p = 0; p < bitMap.getWidth() * bitMap.getHeight(); ++p)
		{
		    int colour = data[p];
		    dataBytes[p * 4]        = (byte)(colour >>> 16);    // R
		    dataBytes[p * 4 + 1]    = (byte)(colour >>> 8);     // G
		    dataBytes[p * 4 + 2]    = (byte) colour;            // B
		    dataBytes[p * 4 + 3]    = (byte)(colour >>> 24);    // A
		}
		
		Texture texture = new Texture();
		texture.mWidth      = bitMap.getWidth();
		texture.mHeight     = bitMap.getHeight();
		texture.mChannels   = 4;
		texture.mData       = dataBytes;
		
		return texture;
    }

}
